//
//  WQMainTabBarController.swift
//  sanwuWeibo
//
//  Created by 王先强 on 15/12/6.
//  Copyright © 2015年 王先强. All rights reserved.
//

import UIKit

class WQMainTabBarController: UITabBarController {
    
    
    //懒加载加号按钮
    private lazy var composeButton: UIButton = UIButton(imageName: "tabbar_compose_icon_add", backgrondImageName: "tabbar_compose_button")
    
    override func viewDidLoad() {
        super.viewDidLoad()
        //添加子控制器
        addChildViewControllers()
        //添加加号按钮
        tabBar.addSubview(composeButton)
    }
    ///  重新布局中间的加号按钮
    override func viewWillAppear(animated: Bool) {

        super.viewWillAppear(animated)
        let count = self.childViewControllers.count
        let width = tabBar.bounds.width / CGFloat(count) - 2
        composeButton.frame = CGRectInset(tabBar.bounds, width * 2, 0)
        tabBar.bringSubviewToFront(composeButton)
        composeButton.addTarget(self, action: "composeViewVcPresent", forControlEvents: .TouchUpInside)
    }
    
}

// MARK: - WQMainTabBarController 分类
extension WQMainTabBarController {
    ///  创建tabbar
    private func addChildViewControllers() {
        tabBar.tintColor = UIColor.orangeColor()
        addChildViewController(WQHomeController(), title: "主页", imageName: "tabbar_home")
        addChildViewController(WQMessageController(), title: "消息", imageName: "tabbar_message_center")
        addChildViewController(UIViewController())
        addChildViewController(WQDiscoverController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(WQProfileController(), title: "我", imageName: "tabbar_profile")
    }
    ///  创建tabbar的子控制器
    private func addChildViewController(vc: UIViewController, title: String, imageName: String) {
        vc.title = title
        vc.tabBarItem.image = UIImage(named: imageName)
        let nav = UINavigationController(rootViewController: vc)
        addChildViewController(nav)
    }
    
    
    @objc private func composeViewVcPresent() {
       let vc = WQComposeController()
        let nav = UINavigationController(rootViewController: vc)
        presentViewController(nav, animated: true, completion: nil)
    }
    
}